package com.itheima.controller;

import com.itheima.common.CommonResult;
import com.itheima.dto.AddAddressBookDto;
import com.itheima.dto.ModifyAddressBookDto;
import com.itheima.dto.SetDefaultDto;
import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YangJiaHao
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @PutMapping("/default")
    public CommonResult setDefault(@RequestBody SetDefaultDto setDefaultDto){
        addressBookService.setDefault(setDefaultDto);
        return CommonResult.success("设置成功");
    }

    @GetMapping("/default")
    public CommonResult getDefault(){
        AddressBook addressBook = addressBookService.getDefault();
        return CommonResult.success(addressBook);
    }

    @PostMapping
    public CommonResult add(@RequestBody AddAddressBookDto dto){
        addressBookService.add(dto);
        return CommonResult.success("添加地址成功");
    }

    @GetMapping("/{id}")
    public CommonResult getById(@PathVariable String id){
        AddressBook addressBook = addressBookService.getById(id);
        return CommonResult.success(addressBook);
    }

    @GetMapping("/list")
    public CommonResult getList(){
        List<AddressBook> addressBooks = addressBookService.getList();
        return CommonResult.success(addressBooks);
    }

    @DeleteMapping
    public CommonResult remove(String ids){
        Boolean result = addressBookService.remove(ids);
        if (!result){
            return CommonResult.error("删除失败");
        }
        return CommonResult.success("删除成功");
    }

    @PutMapping
    public CommonResult modify(@RequestBody ModifyAddressBookDto dto) {
        Boolean result = addressBookService.modify(dto);
        if (!result){
            return CommonResult.error("修改失败");
        }
        return CommonResult.success("修改成功");
    }
}
